//
//  MessageViewController.m
//  EDPS
//
//  Created by dllo on 15/11/27.
//  Copyright © 2015年 张维扬. All rights reserved.
//

#import "MessageViewController.h"
#import "Height.h"
//#import "UMSocial.h"

#import "CoreDataManager.h"
#import "SaveText.h"

//#import "UMSocial.h"
/*
#import "UMSocialAccountManager.h"
#import "UMSocialSnsService.h"
*/


@interface MessageViewController ()<UITextViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic,retain) UITextView *textField;
@property (nonatomic,retain) UIButton *button;
@property (nonatomic,retain) CoreDataManager *manager;
@property (nonatomic,retain) UIImageView *addImagV;
@property (nonatomic,retain) UIView *belowView;
@property (nonatomic,assign) BOOL isImage;

@end

@implementation MessageViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"我的游记";
    self.view.backgroundColor = [UIColor whiteColor];
    self.isImage = 1;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(leftActiong)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
    self.manager = [CoreDataManager ShareInstanceCoreDataManager];
    NSLog(@"路径 %@", [self.manager applicationDocumentsDirectory]);
    [self creatViews];
}
- (void)creatViews
{
    self.automaticallyAdjustsScrollViewInsets
    = NO;
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(20,84, ScreenWidth - 40, ScreenHeight / 6 + ScreenHeight / 3 - 40)];
    backView.layer.borderColor = RGBA(30, 170, 134, 1).CGColor;
    backView.layer.borderWidth = 1;
    [self.view addSubview:backView];
    
    self.textField = [[UITextView alloc] initWithFrame:CGRectMake(20,64 + 20, ScreenWidth - 40, ScreenHeight / 3 )];
    self.textField.layer.borderWidth = 1;
    self.textField.layer.borderColor = RGBA(30, 170, 134, 1).CGColor;
    self.textField.text = @"我的游记";
    self.textField.delegate = self;
    [self.view addSubview:self.textField];
    
    /*
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(0, 0, 100 , 50);
    [self.button setTitle:@"@我的游记" forState:UIControlStateNormal];
    [self.button setTitleColor:RGBA(30, 170, 134, 1) forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(dismiss:) forControlEvents:UIControlEventTouchUpInside];
    [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.textField addSubview:self.button];
    */
    /*
    UIButton *AddButton = [UIButton buttonWithType:UIButtonTypeCustom];
    AddButton.frame = CGRectMake(40, backView.frame.size.height + backView.frame.origin.y - 50, 30, 30);
    [AddButton setImage:[UIImage imageNamed:@"iconfont-tianjia.png"] forState:UIControlStateNormal];
    [AddButton addTarget:self action:@selector(addImage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:AddButton];
    
    */
    UIButton *SaveButton = [UIButton buttonWithType:UIButtonTypeCustom];
    SaveButton.frame = CGRectMake(ScreenWidth - 80, backView.frame.size.height + backView.frame.origin.y - 50, 40, 30);
    SaveButton.backgroundColor = RGBA(30, 170, 134, 1);
    [SaveButton setTitle:@"保存" forState:UIControlStateNormal];
    [SaveButton setTitleColor: [UIColor whiteColor] forState:UIControlStateNormal];
    [SaveButton addTarget:self action:@selector(SaveContent) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:SaveButton];
    /*
    self.belowView = [[UIView alloc] initWithFrame:CGRectMake(20 , backView.frame.size.height + backView.frame.origin.y - 50 + 35, 90, 90)];
    self.belowView.layer.borderColor = RGBA(30, 170, 134, 1).CGColor;
    self.belowView.layer.borderWidth = 1;
    self.belowView.backgroundColor = [UIColor whiteColor];
    self.belowView.hidden = YES;
    [self.view addSubview:self.belowView];
    
    self.addImagV = [[UIImageView alloc] initWithFrame:CGRectMake(30, ScreenHeight / 6 + ScreenHeight / 3 - 40 + 90, 60, 60)];
    [self.view addSubview:self.addImagV];
    */
}
- (void)SaveContent
{
    if ([self.textField.text isEqualToString:@""])
    {
        NSString *alert = @"您输入的内容为空";
        [self creatAlertView:alert];
    }
    else
    {
        NSString *alert = @"保存成功";
        [self creatAlertView:alert];
        SaveText *saveText = [NSEntityDescription insertNewObjectForEntityForName:@"SaveText" inManagedObjectContext:self.manager.managedObjectContext];
        NSString *saveTe = self.textField.text;
        saveText.text = saveTe;
        [self.manager saveContext];
        
        self.textField.text = @"";
    }
}
/*
- (void)dismiss:(UIButton *)button
{
    button.hidden = YES;
}
 */
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
//换行
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    [self.textField resignFirstResponder];
    return YES;
}
//Button隐藏
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    self.button.hidden = YES;
}

- (void)leftActiong
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)creatAlertView:(NSString *)string
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:string preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alert animated:YES completion:nil];
    [self performSelector:@selector(cancle) withObject:alert afterDelay:1];
}
- (void)cancle
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

/*
- (void)addImage
{
    //获取本地相册控制器对象
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    //设置类型
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    //是否允许编辑
    picker.allowsEditing = YES;
    picker.delegate = self;
    //模态显示本地相册
    [self presentViewController:picker animated:YES completion:^{
        
    }];
}

//当用户选择相册的某个图片是触发的方法
//该方法可以获取到选中的图片
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    self.belowView.hidden = NO;
    self.addImagV.image = image;
    self.isImage = !self.isImage;
    //让相册模态消失
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}*/
/*
- (void)shareContent
{
  
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"56429229e0f55a9892005b94"
                                      shareText:@"Just a Moment 带给你想要的一切"
                                     shareImage:[UIImage imageNamed:@"iiconfont-fenxiang"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQzone,UMShareToQQ,UMShareToRenren,UMShareToDouban,UMShareToEmail,UMShareToSms,UMShareToFacebook,UMShareToTwitter,nil]
                                       delegate:nil];
}
*/


@end